package com.yk.filedownload.downloader.DownloadCell;

import com.yk.filedownload.DataTransfer.RedisOp.RedisOperation;
import com.yk.filedownload.DataTransfer.RedisOp.impl.RedisOperationImpl;
import com.yk.filedownload.entity.DataCarrier;

import java.util.Map;

/**
 * Project Name:cykfiledownload
 * File Name:CommonCell
 * Package Name:com.yk.filedownload.downloader.service.DownloadCell
 * Date:2017\12\11 0011
 * Author:dengtianjia
 * Description:
 * Copyright (c) 2017, 重庆云凯科技有限公司 All Rights Reserved.
 */
public abstract class CommonCell implements Runnable{

    public abstract Map getSatus();

    /**
     * 计算线程下载速率
     * @param RealTimeDownSize
     * @param startTime
     * @return
     */
    public Double getDownLoadRate(Integer RealTimeDownSize , Long startTime){
        Long costTime = System.currentTimeMillis() - startTime;
        double time = costTime.doubleValue()/1000;
        double realtime_rate = RealTimeDownSize.doubleValue();
        return realtime_rate/time;
    }

    /**
     * 将数据写入redis内存中
     * @param carrier
     * @param RealTimeDownSize
     */
    public void dataWriteIntoRedis(DataCarrier carrier,Integer RealTimeDownSize){
        RedisOperation redisOperation = new RedisOperationImpl();
        redisOperation.add(carrier.getFileId(),carrier.getCompressData());
        int historyData  = Integer.valueOf(redisOperation.findByKeyString("HistoryDownRate")) + RealTimeDownSize;
        System.out.println(Thread.currentThread().getName()+"下载的数据量为"+historyData);
        redisOperation.add("HistoryDownRate",""+historyData);
        /**
         * 关闭redis的连接操作实例 (----------------需要测试可行性)
         */
        redisOperation.closeConn();
    }

    /**
     * 移出低速下载连接已经下载完的redis数据块
     * @param fileId
     * @param packNum
     */
    public void removeDownloadedDataInRedis(String fileId , int packNum){
        RedisOperation redisOperation = new RedisOperationImpl();
        for(int i = 0 ; i < packNum ; i ++){
            redisOperation.delete(fileId+"@"+i);
        }
    }

    /**
     * 将低速下载连接存入某个位置（kafka或redis）
     * @param url
     */
    public void BackThrLowRateUrl(String url){

    }



}
